编写好实现指定功能的Verilog模块后,需要对其进行仿真来验证模块的正确性,这需要用到EDA开发工具的仿真器,我们选择Xilinx公司的Vivado自带的仿真工具进行仿真。
1. 编写仿真模块
在前面的章节已经学习了为Verilog模块编写基本的测试模块,即testbench的基本步骤和方法。本文不再赘述,直接罗列代码如下。
组合逻辑版calc()模块
//用组合逻辑实现与calc_v2()函数相同的功能
module calc_wire(
input wire [31:0] a,
input wire [31:0] b,
input wire [31:0] c,
output wire [31:0] sum
);
wire [31:0] tmp;
assign tmp = (a+b) - c * 8;
assign sum = tmp;
endmodule
时序逻辑版calc()模块
//用时序逻辑实现与calc_v2()函数相同的功能,计算表达式((a+b) - c * 8)的值
module calc_reg(
input clk,
input wire [31:0] a,
input wire [31:0] b,
input wire [31:0] c,
output reg [31:0] sum
);
//声明一个位宽32bit的reg型变量tmp
reg [31:0] tmp;
//alway块用于对reg型变量赋值tmp
always @(posedge clk) tmp |